package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupbaby.ydd.common.BusinessException;
import com.groupbaby.ydd.common.GetPage;
import com.groupbaby.ydd.common.ResultData;
import com.groupbaby.ydd.entity.YddLink;
import com.groupbaby.ydd.service.IYddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@Slf4j
@RequestMapping("/ydd_link")
public class YddLinkController {

    @Autowired
    private IYddLinkService yddLinkService;

    /**
     * 分页查询
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String type, String page, String limit){

        int linkPage = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);

        Page<YddLink> pageInfo = new Page<>(linkPage, pageSize);

        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddLink::getType, type);

        Page<YddLink> yddLinkPage = yddLinkService.page(pageInfo, queryWrapper);

        int count = yddLinkService.count(queryWrapper);

        List<YddLink> records = yddLinkPage.getRecords();

        com.groupbaby.ydd.common.Page getPage = com.groupbaby.ydd.common.
                Page.init(count,  pageSize,  linkPage, records);
        return ResultData.ok("", getPage);
    }

    /**
     * 按id单个查询, 修改回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id){
        int idCode = Integer.parseInt(id);
        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddLink::getId, idCode);

        YddLink yddLink = yddLinkService.getById(id);
        return ResultData.ok("", yddLink);
    }

    /**
     * 按条件修改
     * @param yddLink
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddLink yddLink){
        if(yddLink == null){
            throw  new BusinessException("信息不能为空");
        }

        yddLinkService.updateById(yddLink);
        return ResultData.ok("","修改成功");
    }

    /**
     * 添加保存
     * @param yddLink
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddLink yddLink) {
        if (yddLink == null) {
            throw new BusinessException("信息不能为空");
        }

        yddLinkService.save(yddLink);
        return ResultData.ok("","添加成功");
    }

    /**
     * 按id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(String id){
        yddLinkService.removeById(id);
        return ResultData.ok("","删除成功");
    }

}
